var express = require('express');
var router = express.Router();
var mongoose = require('mongoose')
var User = require('../models/user')
var Book = require('../models/book')
var Label = require('../models/label')

mongoose.connect('mongodb://localhost:27017/wx')
mongoose.connection.on('connected',()=>{
  console.log("MongoDB conect success")
})
mongoose.connection.on('error',()=>{
  console.log("MongoDB conect error")
})
mongoose.connection.on('disconnected',()=>{
  console.log("MongoDB conect disconnected")
})

/* GET users listing. */
// 获取最新期刊
router.get('/latest', function(req, res, next) {
  User.find((err,doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: 'err',
        result: err.message
      })
    } else {
      // console.log(doc)
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: doc
        })
      }
    }
  })
});

//点赞
router.post('/like',(req,res,next)=>{
  // console.log(req)
  var param = {
    index:req.body.index
  }
  // console.log('param:'+param)
  // console.log('index:'+param.index)
  User.findOne({index:param.index},(err,doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: err.message
      })
    } else {
      if(doc) {
        doc.like_status = 1
        doc.fav_nums++
        console.log('doc:'+doc)
      }
      doc.save((err1,doc1)=>{
        if(err1) {
          res.json({
            status: '1',
            msg: err.message
          })
        } else {
          if(doc1) {
            res.json({
              status: '0',
              msg: '',
              result: 'succ'
            })
          }
        }
      })
    }
  })
}) 

// 取消点赞
router.post('/dislike',(req,res,next)=>{
  console.log(req.body)
  var param = {
    index:req.body.index
  }
  console.log('index:'+param.index)
  User.findOne({index:param.index},(err,doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: err.message
      })
    } else {
      if(doc) {
        doc.like_status = 0
        doc.fav_nums--
      }
      console.log(doc)
      doc.save((err1,doc1)=>{
        if(err1) {
          res.json({
            status: '1',
            msg: err.message
          })
        } else {
          if(doc1) {
            res.json({
              status: '0',
              msg: '',
              result: 'succ'
            })
          }
        }
      })
    }
  })
}) 
// 获取最新热门书籍
router.get('/hot_book', function(req, res, next) {
  Book.find((err,doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: 'err',
        result: err.message
      })
    } else {
      // console.log(doc)
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: doc
        })
      }
    }
  })
});
//获取热门搜索标签
router.get('/get_label',(req, res, next)=>{
  Label.find((err, doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: 'err',
        result: err.message
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: doc
        })
      }
    }
  })
})

// 获取我的喜欢列表
router.get('/get_like_list',(req,res,next)=>{
  User.find({like_status:1},(err, doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: 'err',
        result: err.message
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: doc
        })
      }
    }
  })
})
// 搜索
router.get('/search',(req, res, next)=>{
  // let title = req.params("title"),
  //     author = req.params("author")
    let title = req.query.title,
        author = req.query.author
      console.log(title,author)
  // Book.find({"$or":[{title:/title/},{author:/author/}]},(err, doc)=>{
    Book.find({"$or":[{'title':{$regex:/title/i}},{'author':{$regex:/author/i}}]},(err, doc)=>{
  // Book.find({title:title},(err, doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: 'err',
        result: err.message
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: doc
        })
      }
    }
  })
})
module.exports = router;
